import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import BilibiliQRCodeGenerator 1.0
Item {
    id: window
    width: 600
    height: 400
    visible: true

    // 假设你已经在main.cpp中注册了这个类型
    BilibiliQRCodeGenerator {
        id: qrGenerator

        onQrcodeGenerated: function(success, url, qrcodeKey) {
            if (success) {
                console.log("QR Code generated successfully!")
                console.log("URL:", url)
                console.log("QR Code Key:", qrcodeKey)
            } else {
                console.log("Failed to generate QR code")
            }
        }
    }

    ColumnLayout {
        anchors.centerIn: parent
        spacing: 20

        Text {
            text: "Bilibili QR Code Generator"
            font.pixelSize: 24
            font.bold: true
            Layout.alignment: Qt.AlignHCenter
        }

        Button {
            text: qrGenerator.isLoading ? "生成中..." : "生成二维码"
            enabled: !qrGenerator.isLoading
            Layout.alignment: Qt.AlignHCenter
            onClicked: qrGenerator.generateQRCode()
        }

        Rectangle {
            width: 400
            height: 200
            border.color: "#ccc"
            border.width: 1
            Layout.alignment: Qt.AlignHCenter
            visible: qrGenerator.url !== ""

            ScrollView {
                anchors.fill: parent
                anchors.margins: 10

                ColumnLayout {
                    width: parent.width
                    spacing: 10

                    Text {
                        text: "二维码URL:"
                        font.bold: true
                    }

                    TextArea {
                        text: qrGenerator.url
                        readOnly: true
                        wrapMode: TextArea.Wrap
                        Layout.fillWidth: true
                        selectByMouse: true
                    }

                    Text {
                        text: "二维码Key:"
                        font.bold: true
                    }

                    TextArea {
                        text: qrGenerator.qrcodeKey
                        readOnly: true
                        wrapMode: TextArea.Wrap
                        Layout.fillWidth: true
                        selectByMouse: true
                    }
                }
            }
        }

        Text {
            text: qrGenerator.errorMessage
            color: "red"
            visible: qrGenerator.errorMessage !== ""
            wrapMode: Text.Wrap
            Layout.maximumWidth: 400
            Layout.alignment: Qt.AlignHCenter
        }
    }
}
